package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.dao.impl.UserDaoImpl;
import com.skyc.entity.Address;
import com.skyc.entity.User;
import com.skyc.service.AddressService;
import com.skyc.service.UserService;
import com.skyc.service.impl.AddressServiceImpl;
import com.skyc.service.impl.UserServiceImpl;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/AddressServlet")
public class AddressServlet extends HttpServlet {
	PrintWriter out = null;
	HttpSession session = null;
	User user=null;
	AddressService as=null;
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setHeader("Access-Control-Allow-Origin", "*");
		session = request.getSession();
		ServletContext application = getServletContext();
		out = response.getWriter();
		user=(User) session.getAttribute("user");
		as=new AddressServiceImpl();
		if(user!=null) {//判断是否当前无用户登录
			
			String flag="";
			if(request.getParameter("flag")!=null) {
				flag=request.getParameter("flag");
				if(flag.equals("deleteinfo")) {
					addressDelete(request,response);
				}else if(flag.equals("save")) {
					addsave(request,response);
				}else if(flag.equals("updateD")) {
					updateD(request,response);
				}
			}		
	}else {
		out.print("<script>alert('请先登录账号');location='login.jsp';</script>");
	}
		out.flush();
		out.close();
	}
	private void updateD(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String index=request.getParameter("aindex");
		List<Address> alist=(List<Address>) session.getAttribute("alist");
		boolean flag1=as.UpdateByDefault("1");
		System.out.println(flag1?"其他改为0成功":"失败");	
		boolean flag2=as.UpdateByAddressDefault(alist.get(Integer.valueOf(index)));
		System.out.println(flag2?"设置默认成功":"失败");
		alist=as.QueryByUid(user);
		session.setAttribute("alist", alist);
		 request.getRequestDispatcher("uaddress_show.jsp").forward(request, response);
	}

	private void addsave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String province = "";
		String city = "";
		String area = "";
		String town = "";
		String details="";
		String aname="";
		String isdefault="";
		String uphone="";
		if(request.getParameter("aname")!=null) {
			aname=request.getParameter("aname");
		}
		if (request.getParameter("province") != null) {
			province = request.getParameter("province");
		}
		if (request.getParameter("city") != null) {
			city = request.getParameter("city");
		}
		if (request.getParameter("area") != null) {
			area = request.getParameter("area");
		}
		if (request.getParameter("town") != null) {
			town = request.getParameter("town");
		}
		if(request.getParameter("details")!=null) {
			details=request.getParameter("details");
		}
		if(request.getParameter("udefault")!=null) {
			if("1".equals(request.getParameter("udefault"))) {
				 isdefault=request.getParameter("udefault");
				boolean flag= as.UpdateByDefault("1");
				System.out.println(flag?"修改成功":"修改失败");
				
			}else{
				isdefault="0";	
		}		
		}
		System.out.println(isdefault);
		if(request.getParameter("uphone")!=null) {
			uphone=request.getParameter("uphone");
		}
		String Alladdress=province+city+area+town+details;//地址
			
		Address address = new Address();
		address.setUaddress(Alladdress);
	    address.setAdefault(isdefault);
	    address.setAname(aname);
	    address.setU(user);
	    Address uadd= as.addAddress(address);    
	    List<Address> alist=as.QueryByUid(user);
	    session.setAttribute("alist",alist);
	    request.getRequestDispatcher("uaddress_show.jsp").forward(request, response);
		
	}
		
	private void addressDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String index=request.getParameter("aindex");		
		List<Address> alist=(List<Address>) session.getAttribute("alist");
		boolean deleteflag=as.DeleteByAid(alist.get(Integer.valueOf(index)));
		if(deleteflag) {
			alist=as.QueryByUid(user);
			 session.setAttribute("alist",alist);
			 request.getRequestDispatcher("uaddress_show.jsp").forward(request, response);
		}else {
			out.print("<script>alert('删除失败');history.go(-1);</script>");

		}
		
	}

}
